// Copyright (c) 2010 SuccessFactors, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//     * Redistributions of source code must retain the above
//       copyright notice, this list of conditions and the following
//       disclaimer.
//
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials
//       provided with the distribution.
//
//     * Neither the name of the SuccessFactors, Inc. nor the names of
//       its contributors may be used to endorse or promote products
//       derived from this software without specific prior written
//       permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.

package org.owasp.jxt.webinf;

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * XmlHandler
 *
 * @author Jeffrey Ichnowski
 * @version $Revision: 8 $
 */
public abstract class XmlHandler<State extends Enum> extends DefaultHandler {

    protected StringBuilder _buffer = new StringBuilder();
    protected State _state;
    protected List<State> _stateStack = new ArrayList<State>();

    protected static boolean booleanFor(String arg) throws SAXException {
        if ("true".equals(arg) || "yes".equals(arg)) {
            return true;
        } else if ("false".equals(arg) || "no".equals(arg)) {
            return false;
        } else {
            throw new SAXException("Expected true | false | yes | no, found: "+arg);
        }
    }

    protected XmlHandler(State initialState) {
        _state = initialState;
    }

    @Override
    public final void characters(char[] ch, int start, int length)
        throws SAXException
    {
        _buffer.append(ch, start, length);
    }

    @Override
    public final void startElement(String uri, String localName, String qName, Attributes attrs)
        throws SAXException
    {
        _stateStack.add(_state);
        _state = pushElement(uri, localName, qName, attrs);
        _buffer.setLength(0);
    }

    @Override
    public final void endElement(String uri, String localName, String qName)
        throws SAXException
    {
        String content = _buffer.toString();
        _buffer.setLength(0);
        popElement(uri, localName, qName, content);
        _state = _stateStack.remove(_stateStack.size() - 1);
    }

    public abstract State pushElement(String uri, String localName, String qName, Attributes attrs)
        throws SAXException;

    public abstract void popElement(String uri, String localName, String qName, String content)
        throws SAXException;


} // XmlHandler
